package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.exception.HospitalSystemServiceException;
import com.example.hospitalsystem.mapper.PharmacyMapper;
import com.example.hospitalsystem.pojo.dto.pharmacy.AddPharmacyDto;
import com.example.hospitalsystem.pojo.dto.pharmacy.PharmacyPageQueryDto;
import com.example.hospitalsystem.pojo.entity.Pharmacy;
import com.example.hospitalsystem.pojo.vo.MedicineInfoVO;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.result.ResultCodeEnum;
import com.example.hospitalsystem.service.PharmacyService;
import com.example.hospitalsystem.utils.JwtHelper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PharmacyServiceImpl extends ServiceImpl<PharmacyMapper, Pharmacy> implements PharmacyService {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private PharmacyMapper pharmacyMapper;

    @Override
    public Result<Object> getAllPharmacyInfo(PharmacyPageQueryDto pharmacyPageQueryDto) {
        Page<Pharmacy> pharmacyPage=new Page<>(pharmacyPageQueryDto.getPageNum(),pharmacyPageQueryDto.getPageSize());
        IPage<Pharmacy> pharmacyIPage=pharmacyMapper.selectPage(pharmacyPage,null);
        log.info("----",pharmacyIPage);
        return Result.ok(pharmacyIPage);

    }

    /**
     * 将Pharmacy对象转换为MedicineInfoVO对象
     * @param pharmacy
     * @return
     */
    private MedicineInfoVO convertToMedicineInfoVO(Pharmacy pharmacy) {
        MedicineInfoVO medicineInfoVO = new MedicineInfoVO();
        medicineInfoVO.setMedicineName(pharmacy.getMedicineName());
        medicineInfoVO.setStockQuantity(pharmacy.getStockQuantity());
        medicineInfoVO.setUnitPrice(pharmacy.getUnitPrice());

        return  medicineInfoVO;
    }

    @Override
    public Result<Object> addPharmacy(AddPharmacyDto addPharmacyDto) {

        LambdaQueryWrapper<Pharmacy> pharmacyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        pharmacyLambdaQueryWrapper.eq(Pharmacy::getMedicineName, addPharmacyDto.getMedicineName());
        if (pharmacyMapper.selectOne(pharmacyLambdaQueryWrapper) != null) {
            Result.build(ResultCodeEnum.ILLEGAL_OPERATION, "该药品已存在");
        }
        Pharmacy pharmacy=new Pharmacy();
        BeanUtils.copyProperties(addPharmacyDto,pharmacy);
        int is_success=pharmacyMapper.insert(pharmacy);
        if(is_success > 0){
            return Result.build(null,200,"添加药品成功");
        }else{
            return Result.build(null,200,"添加药品失败");
        }
    }

    @Override
    public Result<Object> deletePharmacy(int id) {
        LambdaQueryWrapper<Pharmacy> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Pharmacy::getMedicineId,id);
        int is_success=pharmacyMapper.delete(lambdaQueryWrapper);
        if(is_success>0){
            return Result.build(null,200,"注销药品成功");
        }else{
            return Result.build(null,200,"注销药品失败");

        }
    }

    /**
     * 搜索药品
     * @param key
     * @return
     */
    @Override
    public List<Pharmacy> searchPharmacy(String key) {
        List<Pharmacy> list = pharmacyMapper.search(key);
        return list;
    }

}
